package com.csw.minDiskFile.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.csw.minDiskFile.dao.CloudFileDao;
import com.csw.minDiskFile.dao.FolderDao;
import com.csw.minDiskFile.entity.CloudFile;
import com.csw.minDiskFile.entity.Folder;
import com.csw.minDiskFile.entity.User;

@Controller
@RequestMapping("vc")
public class ViewController {
	@Autowired
	private FolderDao folderDao;
	@Autowired
	private CloudFileDao cloudFileDao;

	@RequestMapping(value = "")
	public ModelAndView mainPage(HttpSession session) {
		ModelAndView mav = new ModelAndView();
		User user = (User) session.getAttribute("user");
		if (user == null) {
			mav.setViewName("/index");
		} else {
			mav.setViewName("/view/main");
		}
		return mav;
	}

	@RequestMapping("file")
	public ModelAndView filePage(@RequestParam("dir") Integer folderId, Model model, HttpSession session) {
		System.out.println("folderId=" + folderId);
		ModelAndView mav = new ModelAndView();
		try {
			List<Folder> folderList;
			List<CloudFile> fileList;
			if (folderId == 0) {
				User user = (User) session.getAttribute("user");
				if (user == null) {
					mav.setViewName("/index");
					return mav;
				}
				// 列出所有的文件和文件夹
				Folder rootFolder = folderDao.selectRootFolderByUserId(user.getId());
				folderList = folderDao.selectFolderListByFolderId(rootFolder.getId());
				fileList = cloudFileDao.selectFileListByFolderId(rootFolder.getId());
			} else {
				folderList = folderDao.selectFolderListByFolderId(folderId);
				fileList = cloudFileDao.selectFileListByFolderId(folderId);
				Folder fatherFolder = folderDao
						.selectFatherFolderById(folderDao.selectFolderById(folderId).getParentFolder());
				model.addAttribute("fatherFolder", fatherFolder);
			}
			model.addAttribute("folderId", folderId);
			model.addAttribute("folderList", folderList);
			model.addAttribute("fileList", fileList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		mav.setViewName("/view/file");
		return mav;
	}

	@RequestMapping("class")
	public ModelAndView photoPage(@RequestParam("type") String type, Model model, HttpSession session) {
		User user = (User) session.getAttribute("user");
		ModelAndView mav = new ModelAndView();
		try {
			if (user == null) {
				mav.setViewName("/index");
				return mav;
			}
			List<CloudFile> fileList = null;
			if ("photo".equals(type)) {
				fileList = cloudFileDao.selectPhotoByUserId(user.getId());
			} else if ("document".equals(type)) {
				fileList = cloudFileDao.selectDocumentByUserId(user.getId());
			} else if ("movie".equals(type)) {
				fileList = cloudFileDao.selectMovieByUserId(user.getId());
			} else if ("music".equals(type)) {
				fileList = cloudFileDao.selectMusicByUserId(user.getId());
			} else if ("zip".equals(type)) {
				fileList = cloudFileDao.selectZipByUserId(user.getId());
			}
			model.addAttribute("fileList", fileList);
			mav.setViewName("/view/class");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}

	@RequestMapping("public")
	public ModelAndView publicPage(Model model) {

		try {

			List<CloudFile> fileList = cloudFileDao.selectPublicFile();
			model.addAttribute("fileList", fileList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/view/public");
		return mav;
	}
}
